﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using LSA.Framework;

namespace Visuals.Wizard
{
    /// <summary>
    /// Interaction logic for StartWindow.xaml
    /// </summary>
    public partial class FourthStepWindow : Window
    {
        private int filled = 0;
        public FourthStepWindow(bool ner)
        {
            InitializeComponent();
            shouldNer.IsChecked = ner;
        }

        public void ProgressBarFill(int value)
        {
            filled += value;
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => progressBar.Value += value));
        }

        public void ProgressBarEnlarge(int value)
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => progressBar.Maximum += value));
        }

        private async void nextButton_Click(object sender, RoutedEventArgs e)
        {
            nextButton.IsEnabled = false;
            shouldNer.IsEnabled = false;
            progressBar.Visibility = Visibility.Visible;
            progressBar.Value = 0;
            progressBar.Maximum = 0;
            if (shouldNer.IsChecked == true)
            {
                var result = await EntitiesAnalyzer.FindEntities(MainWindow.Object.fileNames, ProgressBarEnlarge, ProgressBarFill);
                await TaskEx.Run(() => Thread.Sleep(2000));

                if (shouldContinue.IsChecked != true)
                MessageBox.Show("Автоматично бяха открити " + result.Count.ToString() + " резултата!");

                MainWindow.Object.Entities = result;
            } else
            {
                MainWindow.Object.Entities = new Dictionary<string, byte>();
            }

            if (shouldContinue.IsChecked == true)
            {
                MainWindow.Object.BuildCorpus();
            }
            
            Close();
        }

        private void prevButton_Click(object sender, RoutedEventArgs e)
        {
            ThirdStepWindow thirdStep = new ThirdStepWindow(false);
            Close();
            thirdStep.ShowDialog();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
